package tyk.preparedstatement;

import com.mysql.cj.x.protobuf.MysqlxCrud;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;

import java.sql.*;
import java.util.*;

/***
 *@title test
 *@description <TODO description class purpose>
 *@author Tang'ya'kang
 *@VERSION 1.0.0
 *@CREATE 2023/5/3 21:31
 **/
public class test {
    public static void main(String[] args) throws Exception {
        //Select();
        //Insert();
        //Update();
        //Delect();
        //For();
    }
    static void Select() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/javaweb","root","123456");
        String sql="select * from t_user where account = ? and password = ? ;";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入登录账号");
        String account=scanner.nextLine();
        System.out.println("请输入登录密码");
        String password=scanner.nextLine();

        preparedStatement.setObject(1,account);
        preparedStatement.setObject(2,password);
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()) System.out.println("登录成功");
        else System.out.println("登录失败");
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
    static void Insert() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/javaweb","root","123456");
        String sql="insert into t_user(account,password,nickname) values(?,?,?);";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入登录账号");
        String account=scanner.nextLine();
        System.out.println("请输入登录密码");
        String password=scanner.nextLine();
        System.out.println("请输入用户名");
        String nickname=scanner.nextLine();

        preparedStatement.setObject(1,account);
        preparedStatement.setObject(2,password);
        preparedStatement.setObject(3,nickname);
        int rows=preparedStatement.executeUpdate();
        if(rows>0) System.out.println("数据插入成功");
        else System.out.println("数据插入失败");
        preparedStatement.close();
        connection.close();
    }
    static void Update() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/javaweb","root","123456");
        String sql="update t_user set nickname = ? where id = ?;";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入id");
        String id=scanner.nextLine();
        System.out.println("请输入用户名");
        String nickname=scanner.nextLine();

        preparedStatement.setObject(1,nickname);
        preparedStatement.setObject(2,id);
        int rows=preparedStatement.executeUpdate();
        if(rows>0) System.out.println("数据修改成功");
        else System.out.println("数据修改失败");
        preparedStatement.close();
        connection.close();
    }
    static void Delect() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/javaweb","root","123456");
        String sql="delete from t_user where id = ? ;";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入id");
        String id=scanner.nextLine();

        preparedStatement.setObject(1,id);
        int rows=preparedStatement.executeUpdate();
        if(rows>0) System.out.println("数据删除成功");
        else System.out.println("数据删除失败");
        preparedStatement.close();
        connection.close();
    }
    static void For() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/javaweb","root","123456");
        String sql="select * from t_user";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        ResultSet resultSet=preparedStatement.executeQuery();
        ResultSetMetaData metaDataet=resultSet.getMetaData();
        List<Map> list=new ArrayList<>();
        while(resultSet.next())
        {
            Map map=new HashMap();
            for(int i=1;i<=metaDataet.getColumnCount();++i)
            {
                Object value=resultSet.getString(i);
                map.put(metaDataet.getColumnLabel(i),value);
            }
            list.add(map);
        }
        System.out.println(list);
    }
}
